package com.zhanhong.opencvdemo.core;

import java.util.HashMap;

public final class Configurator {

    private static final HashMap<String, Object> CONFIGURATIONS = new HashMap<>();

    private Configurator() {
        CONFIGURATIONS.put(ConfigType.READY.name(), false);
    }

    public final void Configure() {
        CONFIGURATIONS.put(ConfigType.READY.name(), true);
    }

    @SuppressWarnings("unchecked")
    public <T> T getConfiguration(Enum<ConfigType> key) {
        isReady();
        return (T) CONFIGURATIONS.get(key.name());
    }

    public void putConfiguration(Enum<ConfigType> key, Object value) {
        CONFIGURATIONS.put(key.name(), value);
    }

    private void isReady() {
        final boolean isReady = (boolean) CONFIGURATIONS.get(ConfigType.READY.name());
        if (!isReady) {
            throw new RuntimeException("Configurator is not ready");
        }
    }

    final HashMap<String, Object> getConfigurations() {
        return CONFIGURATIONS;
    }

    public static Configurator getInstance() {
        return Holder.INSTANCE;
    }

    private static class Holder {
        static final Configurator INSTANCE = new Configurator();
    }

}
